﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AndreySmirnov.BLToolkitUtils.VSPackage.Model.ViewModel
{
	public class ClearTextConnInfoViewModel : ViewModelBase
	{
	    private readonly BLTConnectionInfo m_connVM;

		public ClearTextConnInfoViewModel(BLTConnectionInfo connVM)
		{
			m_connVM = connVM;
		}

	    private void UpdateConnectionData(string valName, string value)
		{
			var res = string.Format("{0}={1};{2}={3}", 
				CredentialHelpers.USER_NAME_KEY, (valName == CredentialHelpers.USER_NAME_KEY) ? value : UserName,
				CredentialHelpers.PASSWORD_KEY, (valName == CredentialHelpers.PASSWORD_KEY) ? value : Password);
			m_connVM.CredentialsData = res;
		}

		public string UserName
		{
			get
			{
				return CredentialHelpers.ParseConnectionData(m_connVM.CredentialsData, CredentialHelpers.USER_NAME_KEY);
			}
			set
			{
				UpdateConnectionData(CredentialHelpers.USER_NAME_KEY, value);
				NotifyPropertyChange("UserName");
			}
		}

		public string Password
		{
			get
			{
                return CredentialHelpers.ParseConnectionData(m_connVM.CredentialsData, CredentialHelpers.PASSWORD_KEY);
			}
			set
			{
				UpdateConnectionData(CredentialHelpers.PASSWORD_KEY, value);
				NotifyPropertyChange("Password");
			}
		}
	}
}
